<?php

/**
 *
 */
namespace Application\Controller;

use Application\Service\RegService;
use Infobird\Controller\BaseController;
use Infobird\Utils\LogUtils;

class RegController extends BaseController
{
    public function indexAction(){

    }

    /**
     * 功能：1、注册保存数据
     */
	public function saveAction(){
		$userName = isset($_REQUEST['userName'])?$_REQUEST['userName']:"";
		$password = isset($_REQUEST['password'])?$_REQUEST['password']:"";
		$email    = isset($_REQUEST['email'])?$_REQUEST['email']:"";
        $insert_dt = date("Y-m-d h:i:s");
		$reg = new RegService();
		$password = md5($password);
		$data = array(
            "user_name"  => $userName,
            "password"   => $password,
            "email"      => $email,
            "is_admin"   => "1",
            "person_id"  => "",
            "insert_dt"  => $insert_dt
        );

		try{
            $result = $reg->save($data);
            echo json_encode(array("result"=>"0","msg"=>"save ok"));
            exit;
        }catch (\Exception $e){
            LogUtils::log("t_reg.log", 'save'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
	}

    /**
     * 功能：1、验证用户名唯一性
     */
	public function userNameAction(){
        $userName = isset($_REQUEST['userName'])?$_REQUEST['userName']:"";
        $reg = new RegService();
        try{
            $result = $reg->userName($userName);
            if($result){
                echo "false";
                exit;
            }else{
                echo "true";
                exit;
            }
        }catch (\Exception $e){
            LogUtils::log("t_reg.log", 'userName'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
    }

    /**
     * 功能：1、验证邮箱的唯一性
     */
    public function emailAction(){
        $email = isset($_REQUEST['email'])?$_REQUEST['email']:"";
        $reg = new RegService();
        try{
            $result = $reg->email($email);
            if($result){
                echo "false";
                exit;
            }else{
                echo "true";
                exit;
            }
        }catch (\Exception $e){
            LogUtils::log("t_reg.log", 'userName'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
    }
}